package com.server.llh.llhserverwork.activity.server;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.TextView;

import com.lelaohui.net.BaseLeLaohuiAcitivity;
import com.lelaohui.net.netmanager.INetRequestManager;
import com.lelaohui.net.netmanager.ServerNetRequestManager;
import com.llh.llhsuppliers.R;
import com.server.llh.llhserverwork.adapter.base.ListFragmentPagerAdapter;
import com.server.llh.llhserverwork.bean.Constants;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ZTF on 2017-03-28.
 * 服务预约
 */

public class LlhQuerySubscribeFragment extends BaseLeLaohuiAcitivity {
    @BindView(R.id.left_btn)
    ImageButton leftBtn;
    @BindView(R.id.titlebar_text)
    TextView titlebarText;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.id_rb_fst)
    RadioButton mFstBtn;
    @BindView(R.id.id_rb_snd)
    RadioButton mSndBtn;
    @BindView(R.id.id_rb_thd)
    RadioButton mThdBtn;
    @BindView(R.id.id_vp_viewpager)
    ViewPager mViewPager;
    private final int FIRST_FRAGMENT = 0;
    private final int SECOND_FRAGMENT = 1;
    private final int THIRD_FRAGMENT = 2;
    private ArrayList<Fragment> list=new ArrayList<Fragment>();

    @Override
    protected int getlayout() {
        return R.layout.llh_query_subscribes;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void doBasess() {

    }

    @Override
    protected INetRequestManager getRequestManager() {
        return ServerNetRequestManager.getInstance();
    }
    @Override
    public void showProgress() {
        try {

        }catch (Exception e){

        }
    }

    @Override
    public void hideProgress() {
        try {

        }catch (Exception e){

        }
    }
    ListFragmentPagerAdapter mPagerAdapter;
    private String userId;
    MyBroadFinishActivity  myBroadFinishActivity =null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        if (userId!=null){
            userId=  getIntent().getStringExtra("userId");
        }
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Constants.BroadCastAction.FINISH_ALL_ACTIVITY);
        myBroadFinishActivity = new MyBroadFinishActivity();
        this.registerReceiver(myBroadFinishActivity, intentFilter);

        mViewPager.setOffscreenPageLimit(1);
        LlhServerSubscribeFragment llhServerSubscribeFragment=new LlhServerSubscribeFragment();
        LlhMySubscribeFragment llhMySubscribeFragment=new LlhMySubscribeFragment();
        LlhMyTaskFragment llhMyTaskFragment=new LlhMyTaskFragment();
        list.add(llhServerSubscribeFragment);
        list.add(llhMySubscribeFragment);
        list.add(llhMyTaskFragment);
        //适配器
        mPagerAdapter = new ListFragmentPagerAdapter(getSupportFragmentManager(), list,3);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setOnPageChangeListener(onPageChangeListener);
    }

    private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            //根据用户选中的按钮修改按钮样式
            switch (position) {
                case FIRST_FRAGMENT:
                    mFstBtn.setChecked(true);
                    mSndBtn.setChecked(false);
                    mThdBtn.setChecked(false);
                    break;

                case SECOND_FRAGMENT:
                    mFstBtn.setChecked(false);
                    mSndBtn.setChecked(true);
                    mThdBtn.setChecked(false);
                    break;

                case THIRD_FRAGMENT:
                    mFstBtn.setChecked(false);
                    mSndBtn.setChecked(false);
                    mThdBtn.setChecked(true);
                    break;
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {}

        @Override
        public void onPageScrollStateChanged(int arg0) {}
    };

    @OnClick({R.id.left_btn, R.id.id_rb_fst, R.id.id_rb_snd, R.id.id_rb_thd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.left_btn:
                onBackPressed();
                break;
            case R.id.id_rb_fst:
                mViewPager.setCurrentItem(FIRST_FRAGMENT);
                break;
            case R.id.id_rb_snd:
                mViewPager.setCurrentItem(SECOND_FRAGMENT);
                break;
            case R.id.id_rb_thd:
                mViewPager.setCurrentItem(THIRD_FRAGMENT);
                break;
        }
    }

    private class MyBroadFinishActivity extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Constants.BroadCastAction.FINISH_ALL_ACTIVITY.equals(action)) {
                 intent = new Intent();
                intent.setAction(Constants.BroadCastAction.FINISH_ALL_ACTIVITY);
                sendBroadcast(intent);

                finish();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    if (myBroadFinishActivity!=null){
        this.unregisterReceiver(myBroadFinishActivity);
    }
    }
}
